<template>
    <div class="app-container">



        <h2 style="margin: auto;text-align: center;">精确盘点</h2>
        <el-form ref="form" :model="form" :rules="rules" size="medium" label-width="100px">
            <h3>基本信息</h3>
            <el-row>
                <el-col :span="5">
                    <el-form-item label="单据主题">
                        <el-input v-model="form.iTitle" clearable :style="{ width: '100%' }"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="5">
                    <el-form-item label="单据编号">
                        <el-input v-model="form.iNum" clearable :style="{ width: '100%' }" :disabled="true"
                            placeholder="保存后自动生成">
                        </el-input>
                    </el-form-item>
                </el-col>

                <el-col :span="5">
                    <el-form-item label="盘点日期">
                        <el-date-picker v-model="form.iTime" type="date" placeholder="选择日期">
                        </el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="5">

                    <el-form-item label="盘点库管">
                        <el-input v-model="form.iMan" clearable :style="{ width: '100%' }">
                        </el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="5">
                    <el-form-item label="关联项目">
                        <el-input v-model="form.IPro" clearable :style="{ width: '100%' }">
                            <template slot="append">
                                <i class="el-icon-search" @click="getUserInfo()"></i>
                            </template>
                        </el-input>
                    </el-form-item>
                </el-col>

            </el-row>
            <el-row>

                <el-col :span="18">
                    <el-form-item label="备注">
                        <el-input type="textarea" :rows="2" placeholder="请输入内容" v-model="form.iDesc">
                        </el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>

                <el-col :span="8">
                    <el-form-item label="盘点仓库">
                        <el-input v-model="form.iBase" clearable :style="{ width: '100%' }"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="盘点产品">
                        <el-input v-model="form.iProgram" clearable :style="{ width: '100%' }"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>

        </el-form>
        <el-dialog :visible.sync="dialogUserInfo" style="width: 100%;height: auto;" :close-on-click-modal="false"
            :title="formTitle">
            <el-button class="filter-item" size="mini" type="success" @click="saveUserInfo">确定</el-button>
            <el-row>
                <el-col :span="4">
                    <!-- <el-tree :data="orgData" :load="getOrgs" :props="defaultProps" :expand-on-click-node="false"
                            lazy @node-click="handleNodeClick" style="margin-top:15px" /> -->
                </el-col>
                <el-col :span="20">
                    <el-table ref="multipleTable" v-loading="listLoading" :data="userList" size="small"
                        style="width: 100%;" @sort-change="sortChange" @selection-change="handleSelChangeUser"
                        @row-click="handleRowClick">
                        <el-table-column type="selection" width="44px"></el-table-column>
                        <el-table-column label="项目名称" prop="proName" sortable="custom" align="center" width="150px">
                            <template slot-scope="{row}">
                                <span class="link-type" @click="handleUpdate(row)">{{ row.proName }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column label="项目编号" prop="orgIdToName" align="center">
                            <template slot-scope="scope">
                                <span>{{ scope.row.organizationNames }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column label="日期" prop="email" sortable="custom" align="center" width="200px">
                            <template slot-scope="scope">
                                <span>{{ scope.row.email }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column label="电话" prop="phoneNumber" sortable="custom" align="center" width="200px">
                            <template slot-scope="scope">
                                <span>{{ scope.row.phoneNumber }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column label="操作" align="center" width="125">
                            <template slot-scope="{row}">
                                <el-button type="primary" size="mini" @click="handleUpdate(row)"
                                    v-permission="['AbpIdentity.Users.Update']" icon="el-icon-edit" />
                                <el-button type="danger" size="mini" @click="handleDelete(row)"
                                    :disabled="row.userName === 'admin'" v-permission="['AbpIdentity.Users.Delete']"
                                    icon="el-icon-delete" />
                            </template>
                        </el-table-column>
                    </el-table>
                    <pagination v-show="totalCount > 0" :total="totalCount" :page.sync="page"
                        :limit.sync="listQuery.MaxResultCount" @pagination="getList" />
                </el-col>
            </el-row>

        </el-dialog>





        <div>
            <h2>盘点明细</h2>
            <el-button size="small" @click="addRow">新增</el-button>
        </div>
        <!--设置的表单-->
        <el-form :model="studentData" ref="data" label-width="auto">
            <el-table border :header-cell-style="{ 'text-align': 'center' }" :cell-style="{ 'text-align': 'center' }"
                :data="studentData" ref="table" style="width: 100%" @selection-change="handleSelectionChange">

                <el-table-column align="center" label="产品名称">
                    <template slot-scope="scope">
                        <!--表格里面嵌套表单-->
                        <el-form-item :prop="scope.$index + '.productName'"
                            :rules="{ required: true, message: '产品名称不能为空', trigger: 'blur' }">
                            <el-input v-model="studentData[scope.$index].productName" autocomplete="off" size="small"
                                placeholder="产品名称"></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>

                <el-table-column align="center" label="产品编号">
                    <template slot-scope="scope">
                        <el-form-item :prop="scope.$index + '.productNum'"
                            :rules="{ required: true, message: '产品编号不能为空', trigger: 'blur' }">

                            <el-input v-model="studentData[scope.$index].productNum" autocomplete="off" size="small"
                                placeholder="产品数量"></el-input>

                        </el-form-item>
                    </template>
                </el-table-column>

                <el-table-column align="center" label="规格型号">
                    <template slot-scope="scope">
                        <el-form-item :prop="scope.$index + '.skuNum'"
                            :rules="{ required: true, message: '规格型号不能为空', trigger: 'blur' }">
                            <el-input v-model="studentData[scope.$index].skuNum" autocomplete="off" size="small"
                                placeholder="规格型号"></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>

                <el-table-column align="center" label="单位">
                    <template slot-scope="scope">
                        <el-form-item :prop="scope.$index + '.pUnit'"
                            :rules="{ required: true, message: '单位不能为空', trigger: 'blur' }">
                            <el-input v-model="studentData[scope.$index].pUnit" autocomplete="off" size="small"
                                placeholder="单位"></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="账面数量">
                    <template slot-scope="scope">
                        <el-form-item :prop="scope.$index + '.faceNum'"
                            :rules="{ required: true, message: '账面数量不能为空', trigger: 'blur' }">


                            <el-input v-model="studentData[scope.$index].faceNum" autocomplete="off" size="small"
                                placeholder="单位"></el-input>


                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="实盘数量">
                    <template slot-scope="scope">
                        <el-form-item :prop="scope.$index + '.reallyNum'"
                            :rules="{ required: true, message: '实盘数量不能为空', trigger: 'blur' }">

                            <el-input v-model="studentData[scope.$index].reallyNum" autocomplete="off" size="small"
                                type='number' placeholder="实盘数量"></el-input>

                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="盈亏数量">
                    <template slot-scope="scope">
                        <el-form-item :prop="scope.$index + '.shopNum'">
                            <el-input v-model="studentData[scope.$index].reallyNum - studentData[scope.$index].faceNum"
                                autocomplete="off" size="small" placeholder="盈亏数量"></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>

                <el-table-column align="center" label="单价">
                    <template slot-scope="scope">
                        <el-form-item :prop="scope.$index + '.proPrice'"
                            :rules="{ required: true, message: '单价不能为空', trigger: 'blur' }">



                            <el-input v-model="studentData[scope.$index].proPrice" autocomplete="off" size="small"
                                type='number' placeholder="单价"></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="盈亏金额">
                    <template slot-scope="scope">
                        <el-form-item :prop="scope.$index + '.shopPrice'">
                            <el-input
                                v-model="(studentData[scope.$index].reallyNum - studentData[scope.$index].faceNum) * studentData[scope.$index].proPrice"
                                autocomplete="off" size="small" placeholder="盈亏金额"></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="仓库名称">
                    <template slot-scope="scope">
                        <el-form-item :prop="scope.$index + '.baseName'"
                            :rules="{ required: true, message: '仓库名称不能为空', trigger: 'blur' }">
                            <el-input v-model="studentData[scope.$index].baseName" autocomplete="off" size="small"
                                placeholder="仓库名称"></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="备注">
                    <template slot-scope="scope">
                        <el-form-item :prop="scope.$index + '.desc'"
                            :rules="{ required: true, message: '备注不能为空', trigger: 'blur' }">
                            <el-input v-model="studentData[scope.$index].desc" autocomplete="off" size="small"
                                placeholder="备注"></el-input>
                        </el-form-item>
                    </template>
                </el-table-column>



                <el-table-column fixed="right" label="操作" width="100">
                    <template slot-scope="scope">
                        <el-button @click="handleDeleteRow(studentData[scope.$index])" type="text"
                            size="small">删除</el-button>
                    </template>
                </el-table-column>

            </el-table>
        </el-form><br />
        <div slot="footer">
            <el-button size="small" type="text" @click="cancel">取消</el-button>
            <el-button size="small" v-loading="formLoading" type="primary" @click="save">确认</el-button>
        </div>
    </div>


    </div>
</template>
<script>
import Pagination from "@/components/Pagination";
import permission from "@/directive/permission/index.js";
import { cloneDeep } from "lodash";


export default {
    name: 'Book',
    components: {
        Pagination
    },
    directives: {
        permission
    },
    props: [],
    data() {
        return {
            studentData: [],
            form: {
                id: null,
                iTitle: null,
                iNum: null,
                iTime: Date.now(),
                iMan: null,
                iPro: null,
                iDesc: null,
                iBase: null,
                iProgram: null,
                createOrUpdateInvenDto: []

            },


            rules: {
                name: [{
                    required: true,
                    message: '请输入名称',
                    trigger: 'blur'
                }],
                description: [{
                    required: true,
                    message: '请输入描述',
                    trigger: 'blur'
                }],
                price: [{
                    required: true,
                    message: '价格',
                    trigger: 'blur'
                }],
            },

            userList: [],
            totalCount: 0,
            listLoading: true,
            formLoading: false,
            listQuery: {
                Filter: '',
                Sorting: '',
                SkipCount: 0,
                MaxResultCount: 10
            },
            page: 1,
            dialogFormVisible: false,
            dialogUserInfo: false,
            multipleSelection: [],
            formTitle: '',
            isEdit: false,
        }
    },
    computed: {},
    watch: {},
    created() {
        this.getList()
    },
    mounted() { },
    methods: {
        getUserInfo() {
            this.dialogUserInfo = true;
            this.formTitle = "用户";
            this.$axios.gets("/api/business/produce?Filter=&Sorting=&SkipCount=0&MaxResultCount=10")
                .then((response) => {
                    this.userList = response.items;
                });
        },
        handleSelChangeUser(val) {
            this.form.IPro = val[0].proName;
        },
        // handleSelectionChange(val) {
        //     val.forEach((i) => {
        //         if (!this.AddorUpd) {
        //             this.form.createOrUpdateInvenDto.push({
        //                 id: "",

        //                 productName: i.productName,
        //                 productNum: i.productNum,
        //                 skuNum: i.skuNum,
        //             })
        //         }
        //     })
        // },
        saveUserInfo() {
            this.dialogUserInfo = false;

        },


        /**
    * 新增行
    */
        addRow() {
            let index = this.studentData.length;
            this.studentData.push({

                productName: null,
                productNum: null,
                skuNum: null,
                pUnit: null,
                faceNum: null,
                reallyNum: null,

                proPrice: null,

                baseName: null,
                desc: null
            });
        },

        /**
         * 删除行
         * @param row
         */
        handleDeleteRow(row) {
            let datas = this.studentData;
            for (var i = 0; i < datas.length; i++) {
                if (datas[i].key == row.key) {
                    datas.splice(i, 1);
                }
            }
        },

        /**
         * 提交
         */
        submit() {
            this.$refs["data"].validate(valid => {
                //valid为true，表示表单都已经验证通过，若为false，说明存在表单验证失败
                if (valid) {
                    save(this.studentData).then(response => {
                        this.$message({
                            message: '提交成功',
                            type: 'success'
                        });
                    });
                }
            });
        },

        /**
         * 重置
         */
        resetForm() {
            let datas = this.studentData;
            for (var i = 0; i < datas.length; i++) {
                datas[i].name = '';
                datas[i].age = '';
                datas[i].sex = '';
            }
        },

        getListStatus(items, ids) {
            this.$axios.posts('api/business/workflow/node-status', ids).then(response => {
                items.forEach(element => { response.items.filter((i) => { if (i.entityId === element.id) { element.status = i.status; element.nodeId = i.nodeId; element.nodeStatus = i.nodeName } }) })
                this.userList = items
                this.listLoading = false;
            });
        },
        getList() {
            this.listLoading = true;
            this.listQuery.SkipCount = (this.page - 1) * this.listQuery.MaxResultCount;
            this.$axios.gets('api/business/produce', this.listQuery).then(response => {
                this.totalCount = response.items;
                this.getListStatus(response.items, response.items.map(_ => _.id))
            });
        },





        fetchData(id) {
            this.$axios.gets('/api/business/invenk/' + id).then(response => {
                this.form = response;
            });
        },
        handleFilter() {
            this.page = 1;
            this.getList();
        },
        handleCreate() {
            this.formTitle = '新增图书';
            this.isEdit = false;
            this.dialogFormVisible = true;
        },
        handleDelete(row) {
            var params = [];
            let alert = '';
            if (row) {
                params.push(row.id);
                alert = row.name;
            }
            else {
                if (this.multipleSelection.length === 0) {
                    this.$message({
                        message: '未选择',
                        type: 'warning'
                    });
                    return;
                }
                this.multipleSelection.forEach(element => {
                    let id = element.id;
                    params.push(id);
                });
                alert = '选中项';
            }
            this.$confirm('是否删除' + alert + '?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$axios.posts('/api/business/invenk/delete', params).then(response => {
                    this.$notify({
                        title: '成功',
                        message: '删除成功',
                        type: 'success',
                        duration: 2000
                    });
                    this.getList();
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        handleCheck() {
            if (this.multipleSelection.length === 0) {
                this.$message({
                    message: '未选择',
                    type: 'warning'
                });
                return;
            }
            if (this.multipleSelection.length > 1) {
                this.$message({
                    message: '暂不支持批量审核',
                    type: 'warning'
                });
                return;
            }
            this.$confirm('是否审核所选项?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$axios.puts('api/business/workflow/do/' + this.multipleSelection[0].id, { data: JSON.stringify(this.multipleSelection[0]) }).then(response => {
                    this.$notify({
                        title: '成功',
                        message: '审核成功',
                        type: 'success',
                        duration: 2000
                    });
                    this.getList();
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消审核'
                });
            });
        },
        findFlow(row) {
            this.$router.push({ path: "/tool/flowDisplay/" + row.nodeId });
        },
        handleUpdate(row) {
            this.formTitle = '修改图书';
            this.isEdit = true;
            if (row) {
                this.fetchData(row.id);
                this.dialogFormVisible = true;
            }
            else {
                if (this.multipleSelection.length != 1) {
                    this.$message({
                        message: '编辑必须选择单行',
                        type: 'warning'
                    });
                    return;
                }
                else {
                    this.fetchData(this.multipleSelection[0].id);
                    this.dialogFormVisible = true;
                }
            }
        },
        save() {
            //数组与对象转换
            let goodList = [];
            this.studentData.forEach(element => {
                goodList.push({
                    id: element.id,
                    productName: element.productName,
                    productNum: element.productNum,
                    skuNum: element.skuNum,
                    pUnit: element.pUnit,
                    faceNum: element.faceNum,
                    reallyNum: element.reallyNum,

                    proPrice: element.proPrice,

                    baseName: element.baseName,
                    desc: element.desc



                });
            });
            this.form.createOrUpdateInvenDto = goodList;

            console.log(this.form)





            this.$refs.form.validate(valid => {
                if (valid) {
                    this.formLoading = true;
                    this.form.roleNames = this.checkedRole;
                    if (this.isEdit) {
                        this.$axios.posts('/api/business/invenk/data-post', this.form).then(response => {
                            this.formLoading = false;
                            this.$notify({
                                title: '成功',
                                message: '更新成功',
                                type: 'success',
                                duration: 2000
                            });
                            this.dialogFormVisible = false;
                            this.getList();
                        }).catch(() => {
                            this.formLoading = false;
                        });
                    }
                    else {
                        this.$axios.posts('/api/business/invenk/data-post', this.form).then(response => {
                            this.formLoading = false;
                            this.$notify({
                                title: '成功',
                                message: '新增成功',
                                type: 'success',
                                duration: 2000
                            });
                            this.dialogFormVisible = false;
                            this.getList();
                        }).catch(() => {
                            this.formLoading = false;
                        });
                    }
                }
            });
        },
        sortChange(data) {
            const {
                prop,
                order
            } = data;
            if (!prop || !order) {
                this.handleFilter();
                return;
            }
            this.listQuery.Sorting = prop + ' ' + order;
            this.handleFilter();
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        handleRowClick(row, column, event) {
            this.$refs.multipleTable.clearSelection();
            this.$refs.multipleTable.toggleRowSelection(row);
        },
        cancel() {
            this.form = Object.assign({}, defaultForm);
            this.dialogFormVisible = false;
            this.$refs.form.clearValidate();
        },
    }
}

</script>
<style></style>
